#pragma once

#include <QWidget>
#include "ui_CustomPlotEx.h"

#define MAX_GRAPH_NUM 8
#define MIN_GRAPH_NUM 0

enum MouseFunc
{
	NoFunc = 0,
	SecPt = 1,
	DrawLine,
};

class CustomPlotEx : public QWidget
{
	Q_OBJECT
public:
	CustomPlotEx(QWidget *parent = Q_NULLPTR);
	~CustomPlotEx();
protected:
	virtual void keyPressEvent(QKeyEvent *event);

private:
	Ui::CustomPlotEx *ui;
	QColor m_colors[8] = { QColor(Qt::blue),QColor(Qt::red),QColor(Qt::black), QColor(Qt::cyan),QColor(Qt::green),QColor(Qt::yellow),QColor(Qt::magenta),QColor::fromRgb(10,221,246) };
	uint8_t m_graphNum;
	QMenu m_RbtnMenu;
	QCPGraph *m_curSelectedGraph;
	QCPItemTracer *m_tracer;
	QCPItemText *m_tracerLabel; 
	unsigned int m_xSliderPos, m_ySliderPos;
	
	bool m_isButtonPress = false;

	void ConnectSlots();
	void TracerInit();
	void plotInit();
	void RbtnMenuInit();
	void AXisRangeChange(QCPAxis *axis, QCPAxis::SelectablePart part, QMouseEvent *event);
	void ShowRbtnMenu(const QPoint &pos);
	QCPGraphData* getPointByValue(QPoint &point, QCPGraph * &returnGraph);
	QCPGraphData* FindMousePoint(QCPGraph * plot, double xMin, double xMax, double yMin, double yMax, QPoint& point);

	void onChartMouseDoubleClicked(QMouseEvent *event);
	void onChartMousePress(QMouseEvent *event);
	void onMouseWheel(QWheelEvent *event);
	void updateMouseSelectedPoint(QMouseEvent *event);

	void onyAxisZoomOut();
	void onyAxisZoomIn();
	void onxAxisZoomOut();
	void onxAxisZoomIn();
	void onHorizontalScrollBarSliderMoved(int pos);
	void onVerticalScrollBarSliderMoved(int pos);

	void updateMouseButton();
	void updateButtonStateTosecPt(bool checked);

	void copyGraph();
	void pasteGraph();
public:
	QCustomPlot *GetChart();
	QCPGraph *AddGraph(QString graph_name);
	QCPRange m_xZoomRange, m_yZoomRange;
	void RemoveGraph(QCPGraph *grap);
	void RemoveGraph(int index);
	void replot();
	void clearGraps();
	void autoRange();
	void lineDrawEnable(bool enable = true);
	void curveDrawEnable(bool enable = true);
	void setShowRange(const QCPRange& xRange, const QCPRange& yRange);
};
